package com.glorysoft.hlgdpda.ui.materialandtool.view

import android.os.Bundle
import android.view.View
import com.glorysoft.hlgdpda.BR
import com.glorysoft.hlgdpda.R
import com.glorysoft.hlgdpda.base.BaseActivity
import com.glorysoft.hlgdpda.databinding.ActivityMaterialDownBinding
import com.glorysoft.hlgdpda.entity.ui.ToastMessageBean
import com.glorysoft.hlgdpda.ui.materialandtool.viewmodel.MaterialViewModel
import com.glorysoft.hlgdpda.utils.PopWindowsUtils
import kotlinx.android.synthetic.main.activity_material_down.*
import kotlinx.android.synthetic.main.activity_material_down.etEqpWorkID

/**
 * FileName: MaterialUpActivity
 * Author: XJH
 * Date: 2021/5/14 17:48
 * Description:
 * History:
 */
class MaterialDownActivity : BaseActivity<MaterialViewModel, ActivityMaterialDownBinding>(),
    View.OnClickListener {

    private var materialLotPop : PopWindowsUtils.GeneralPop? = null

    override fun layoutId(): Int {
        return R.layout.activity_material_down
    }

    override fun initVariableId(): Int {
        return BR.materialViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle("物料下机")
        viewModel.initViewModel()
        setRightListener("解绑"){
            viewModel.mLotKitting("DEASSIGN")
        }
        flMaterialLotList.setOnClickListener(this)
    }

    override fun initData() {
        initEdit(etEqpWorkID,object : EditTextListener{
            override fun onTextChangeListener(s: String) {
                viewModel.eqpID = s
                viewModel.searchMaterial()
            }

        })

        viewModel.defUI.callObserve.observe(this){
            viewModel.defUI.toastEvent.value = ToastMessageBean("物料下机成功",true)
            tvMaterialLot.text = ""
            etEqpWorkID.setText("")
        }
    }

    override fun onClick(v: View?) {
        if (viewModel.materialLotList.size <= 0){
            viewModel.defUI.toastEvent.value = ToastMessageBean("未获取到物料批次信息",false)
            return
        }

        if (materialLotPop == null){
            materialLotPop = PopWindowsUtils.getOneChoosePop(this, flMaterialLotList.width, viewModel.materialLotList, ivMaterialLotArrow,object : PopWindowsUtils.PopTextAndPositionCallback{
                override fun onSuccess(codeS: String, potion: Int) {
                    tvMaterialLot.text = codeS
                    viewModel.materialLot = codeS
                }

            })

        }

        if (materialLotPop!!.isShowing) {
            materialLotPop?.dismiss()
        } else {
            PopWindowsUtils.setArrowShow(ivMaterialLotArrow)
            materialLotPop?.showAsDropDown(flMaterialLotList, 0, 0)
        }
    }
}